package com.archive.forum.controller;

import com.archive.forum.cache.TagCache;
import com.archive.forum.dto.PostDTO;
import com.archive.forum.model.PostWithBLOBs;
import com.archive.forum.model.User;
import com.archive.forum.service.PostService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class PublishController {
    @Autowired
    private PostService postService;
    //根据id编辑帖子
    @GetMapping("/publish/{id}")
    public String edit(@PathVariable(name = "id")Integer id,HttpServletRequest request,Model model) {
        PostDTO postDTO = postService.getById(id);
        User user =  (User) request.getSession().getAttribute("user");
        if (!postDTO.getCreator().equals(user.getId())) {
            model.addAttribute("error", "只有帖子的创作者才能编辑帖子");
        } else {
            model.addAttribute("title", postDTO.getTitle());
            model.addAttribute("description", postDTO.getDescription());
            model.addAttribute("tag", postDTO.getTag());
            model.addAttribute("id", postDTO.getId());

            model.addAttribute("tags", TagCache.get());
        }
        return "publish";
    }
    //发布页
    @GetMapping("/publish")
    public String publish(Model model){
        model.addAttribute("tags", TagCache.get());
        return "publish";
    }
    //发布内容
    @PostMapping("/publish")
    public String doPublish(
            @RequestParam(name = "title",required = false) String title,
            @RequestParam(name = "description",required = false) String description,
            @RequestParam(name = "tag",required = false) String tag,
            @RequestParam(name = "id",required = false) Integer id,
            @RequestParam(name = "plainText",required = false) String plainText,
            HttpServletRequest request,
            Model model) {
        model.addAttribute("tags", TagCache.get());
        User user =  (User) request.getSession().getAttribute("user");
        if(title== null || title.isEmpty() || description== null || description.isEmpty()|| tag== null || tag.isEmpty()){
            model.addAttribute("error","内容不完整");
            return "publish";
        }
        if(user == null){
            model.addAttribute("error","用户未登录");
            return "publish";
        }
        PostWithBLOBs post = new PostWithBLOBs();
        post.setTitle(title);
        post.setPlainText(plainText);
        post.setDescription(description);
        post.setTag(tag);
        post.setCreator(user.getId());

        if (id != null) {
            PostDTO postDTO = postService.getById(id);
            if (!postDTO.getCreator().equals(user.getId())) {
                model.addAttribute("error", "只有文章的创作者才能编辑文章");
                return "publish";
            } else {
                post.setId(id);
            }
        }

        Integer postId = postService.createOrUpdate(post);


        return "redirect:/post/" + postId;
    }
}
